package com.xiaoman.read.file;

import com.xiaoman.read.config.Config;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

public class FileFactory {
    public void read(String url) {
        if (url.endsWith(".pdf")) {
            new PdfFile().read(url);
        }
        if (url.endsWith(".epub")) {
            new EpubFile().read(url);
        }
        // 设置文本信息
        try {
            java.io.File file = new File(url);
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
            randomAccessFile.seek(0);
            byte[] bytes = new byte[1024 * 1024];
            int readSize = randomAccessFile.read(bytes);
            byte[] copy = new byte[readSize];
            System.arraycopy(bytes, 0, copy, 0, readSize);
            String str = new String(copy, StandardCharsets.UTF_8);
            // 设置内容
            Config.readUI.getTextContent().setText(str);
        } catch (Exception ignore) {
        }
    }

    public void displayCurrentChapter() {
        if (Config.getFileType().equals("pdf")) {
            new PdfFile().displayCurrentChapter();
        }
        if (Config.getFileType().equals("epub")) {
            new EpubFile().displayCurrentChapter();
        }
    }

}
